using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace DesktopWidgetApp.UI.ViewModels
{
    /// <summary>
    /// 任务状态枚举
    /// </summary>
    public enum TaskStatus
    {
        Todo,
        InProgress,
        Completed,
        Blocked
    }

    /// <summary>
    /// 任务优先级枚举
    /// </summary>
    public enum Priority
    {
        Low,
        Medium,
        High,
        Urgent
    }

    /// <summary>
    /// 任务项视图模型
    /// </summary>
    public class TaskItemViewModel : INotifyPropertyChanged
    {
        private string _id;
        private string _title;
        private string _description;
        private TaskStatus _status;
        private Priority _priority;
        private DateTime _dueDate;
        private string _tags;
        private bool _isSelected;

        public string Id
        {
            get => _id;
            set => SetProperty(ref _id, value);
        }

        public string Title
        {
            get => _title;
            set => SetProperty(ref _title, value);
        }

        public string Description
        {
            get => _description;
            set => SetProperty(ref _description, value);
        }

        public TaskStatus Status
        {
            get => _status;
            set => SetProperty(ref _status, value);
        }

        public Priority Priority
        {
            get => _priority;
            set => SetProperty(ref _priority, value);
        }

        public DateTime DueDate
        {
            get => _dueDate;
            set => SetProperty(ref _dueDate, value);
        }

        public string Tags
        {
            get => _tags;
            set => SetProperty(ref _tags, value);
        }

        // 用于DataGrid中选择任务
        public bool IsSelected
        {
            get => _isSelected;
            set => SetProperty(ref _isSelected, value);
        }

        // 用于UI显示的优先级文本
        public string PriorityDisplayName => GetPriorityDisplayName();

        // 用于UI显示的状态文本
        public string StatusDisplayName => GetStatusDisplayName();

        // 用于UI显示的截止日期文本
        public string DueDateDisplay => DueDate.ToString("yyyy-MM-dd");

        private string GetPriorityDisplayName()
        {
            switch (Priority)
            {
                case Priority.Low: return "低";
                case Priority.Medium: return "中";
                case Priority.High: return "高";
                case Priority.Urgent: return "紧急";
                default: return Priority.ToString();
            }
        }

        private string GetStatusDisplayName()
        {
            switch (Status)
            {
                case TaskStatus.Todo: return "待办";
                case TaskStatus.InProgress: return "进行中";
                case TaskStatus.Completed: return "已完成";
                case TaskStatus.Blocked: return "受阻";
                default: return Status.ToString();
            }
        }

        // 创建任务副本
        public TaskItemViewModel Clone()
        {
            return new TaskItemViewModel
            {
                Id = this.Id,
                Title = this.Title,
                Description = this.Description,
                Status = this.Status,
                Priority = this.Priority,
                DueDate = this.DueDate,
                Tags = this.Tags
            };
        }

        #region INotifyPropertyChanged 实现

        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
        {
            if (!Equals(field, newValue))
            {
                field = newValue;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                
                // 更新相关的显示属性
                if (propertyName == nameof(Priority))
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PriorityDisplayName)));
                
                if (propertyName == nameof(Status))
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(StatusDisplayName)));
                
                if (propertyName == nameof(DueDate))
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(DueDateDisplay)));
                
                return true;
            }
            return false;
        }

        #endregion
    }
}